<?php

class SiteController extends CController
{
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index');
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		if(!Yii::app()->request->isAjaxRequest||!isset($_POST['username'])||!isset($_POST['password'])) return;
		$identity=new UserIdentity($_POST['username'],$_POST['password']);
		$identity->authenticate();
		$flag=$identity->errorCode==UserIdentity::ERROR_NONE?true:false;
		if($flag){
			Yii::app()->user->login($identity,0);
		}
		echo CJSON::encode(array('ok'=>$flag,'linkman'=>$flag?Yii::app()->session['linkman']:''));
	}
	
	public function actionLogin_page(){
		$form=new LoginForm;
		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$form->attributes=$_POST['LoginForm'];
			// validate user input and redirect to previous page if valid
			if($form->validate())
				$this->redirect(array('business/my_jiajia'));
		}
		// display the login form
		$this->render('login',array('form'=>$form));
	}

	/**
	 * Logout the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		Yii::app()->runController('site/index');	
	}
	
	public function actionToMyJiaJia(){
		$this->redirect(array('business/my_jiajia'));
	}
	
	public function actionMerchants(){
		$this->render('merchants');
	}
}